package com.itrus.ica;

import com.itrus.serviceframe.DataPacket;
import com.itrus.serviceframe.NVRepository;
import com.itrus.serviceframe.NameValuePair;
import com.itrus.util.RegexUtils;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class IcaConnectionImpl
  implements IcaConnection
{
  private static Log log = LogFactory.getLog(IcaConnectionImpl.class);
  private String characterEncoding;
  private String icaHost;
  private int icaPort;
  private Socket sckClient = null;
  private int timeOut;
  private int version = 0;

  public IcaConnectionImpl(String paramString)
    throws UnknownHostException, IOException
  {
    if (!RegexUtils.matches(paramString, "^ica://[^:]*:.*"))
      throw new IOException("无效的ICA URL：" + paramString);
    log.info("ICA服务：URL=[" + paramString + "]");
    parseIcaUrl(paramString);
    this.sckClient = new Socket(this.icaHost, this.icaPort);
    if (this.timeOut > 0)
      this.sckClient.setSoTimeout(1000 * this.timeOut);
  }

  public IcaConnectionImpl(String paramString, int paramInt)
    throws UnknownHostException, IOException
  {
    log.info("连接ICA服务：Host=[" + paramString + "], raPort=[" + paramInt + "]");
    this.sckClient = new Socket(paramString, paramInt);
  }

  private void parseIcaUrl(String paramString)
  {
    this.icaHost = RegexUtils.exceptMatches(paramString, "^ica://|:.*");
    this.icaPort = Integer.parseInt(RegexUtils.exceptMatches(paramString, "^ica://.*:|/.*"));
    String str1 = RegexUtils.exceptMatches(paramString, "^ica://.*:[^/]*/?");
    Properties localProperties = new Properties();
    String[] arrayOfString;
    if (str1 != null)
      arrayOfString = str1.split("&");
    for (int i = 0; ; i++)
    {
      if (i >= arrayOfString.length)
      {
        String str2 = localProperties.getProperty("version");
        if (str2 != null)
          this.version = Integer.parseInt(str2);
        log.debug("IcaApi ServiceFrame版本 = [" + this.version + "]");
        this.characterEncoding = localProperties.getProperty("characterEncoding");
        String str3 = localProperties.getProperty("timeout");
        if (str3 != null)
          this.timeOut = Integer.parseInt(str3);
        return;
      }
      String str4 = arrayOfString[i].trim();
      int j = str4.indexOf("=");
      if (j > 0)
        localProperties.setProperty(str4.substring(0, j), str4.substring(j + 1));
    }
  }

  private DataPacket requestDataPacket(Map paramMap)
    throws IOException
  {
    Iterator localIterator = paramMap.entrySet().iterator();
    DataPacket localDataPacket = new DataPacket(this.version);
    while (true)
    {
      if (!localIterator.hasNext())
      {
        localDataPacket.serialize(this.sckClient.getOutputStream());
        int i = this.version;
        boolean bool = false;
        if (i == 2)
          bool = true;
        return DataPacket.getInstance(this.sckClient.getInputStream(), bool);
      }
      Map.Entry localEntry = (Map.Entry)localIterator.next();
      String str1 = (String)localEntry.getKey();
      String str2 = (String)localEntry.getValue();
      if (this.characterEncoding != null)
      {
        byte[] arrayOfByte = str2.getBytes(this.characterEncoding);
        localDataPacket.getNVRepository().add(new NameValuePair(str1, arrayOfByte));
        log.debug("IN(" + this.characterEncoding + "): [" + str1 + "]=[" + new String(arrayOfByte, this.characterEncoding) + "]");
      }
      else
      {
        localDataPacket.getNVRepository().add(new NameValuePair(str1, str2));
        log.debug("IN: [" + str1 + "]=[" + str2 + "]");
      }
    }
  }

  public void close()
  {
    try
    {
      this.sckClient.close();
      log.debug("关闭IcaConnection连接");
      return;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public long request(LinkedHashMap paramLinkedHashMap1, LinkedHashMap paramLinkedHashMap2)
    throws IOException
  {
    Iterator localIterator = paramLinkedHashMap1.entrySet().iterator();
    DataPacket localDataPacket1 = new DataPacket(this.version);
    DataPacket localDataPacket2;
    while (true)
    {
      if (!localIterator.hasNext())
      {
        localDataPacket1.serialize(this.sckClient.getOutputStream());
        int i = this.version;
        boolean bool = false;
        if (i == 2)
          bool = true;
        localDataPacket2 = DataPacket.getInstance(this.sckClient.getInputStream(), bool);
        long l = localDataPacket2.getStatus();
        if (l == 0L)
          break;
        return l;
      }
      Map.Entry localEntry = (Map.Entry)localIterator.next();
      String str1 = (String)localEntry.getKey();
      byte[] arrayOfByte = (byte[])localEntry.getValue();
      localDataPacket1.getNVRepository().add(new NameValuePair(str1, arrayOfByte));
      log.debug("IN: [" + str1 + "]=[binarydata:" + arrayOfByte.length + "]");
    }
    NVRepository localNVRepository = localDataPacket2.getNVRepository();
    for (int j = 0; ; j++)
    {
      if (j >= localNVRepository.size())
        return localDataPacket2.getStatus();
      NameValuePair localNameValuePair = localNVRepository.elementAt(j);
      String str2 = localNameValuePair.getName();
      log.debug("OUT: [" + str2 + "]=[binarydata:" + localNameValuePair.getValue().length + "]");
      paramLinkedHashMap2.put(str2, localNameValuePair.getValue());
    }
  }

  public long request(Map paramMap1, Collection paramCollection, PipeInteger paramPipeInteger, Map paramMap2)
    throws IOException
  {
    String str1 = (String)paramMap1.get("NEXT_RESULT_TAG");
    String str2 = (String)paramMap1.get("TOTAL_TAG");
    long l;
    if (str1 == null)
      l = 2L;
    DataPacket localDataPacket;
    do
    {
      return l;
      if (str2 == null)
        return 3L;
      localDataPacket = requestDataPacket(paramMap1);
      l = localDataPacket.getStatus();
    }
    while (l != 0L);
    NVRepository localNVRepository = localDataPacket.getNVRepository();
    Properties localProperties = new Properties();
    int i = 0;
    if (i >= localNVRepository.size())
      return localDataPacket.getStatus();
    NameValuePair localNameValuePair = localNVRepository.elementAt(i);
    String str3 = localNameValuePair.getName();
    String str4;
    if (this.characterEncoding != null)
    {
      str4 = new String(localNameValuePair.getValue(), this.characterEncoding);
      log.debug("OUT(" + this.characterEncoding + "): [" + str3 + "]=[" + str4 + "]");
      label202: if (!str3.equals(str1))
        break label295;
      localProperties = new Properties();
      localProperties.setProperty(str1, str4);
    }
    while (true)
    {
      i++;
      break;
      str4 = new String(localNameValuePair.getValue());
      log.debug("OUT: [" + str3 + "]=[" + str4 + "]");
      break label202;
      label295: localProperties.setProperty(str3, str4);
      if (str3.equals(str2))
      {
        paramCollection.add(localProperties);
        paramPipeInteger.setValue(Integer.parseInt(str4));
      }
      else if ((str3.equals("SQL_RECORDS_COUNT")) || (str3.equals("SQL_RECORDS_INDEX")) || (str3.equals("SQL_SCRIPT_RUNTIME")))
      {
        paramMap2.put(str3, str4);
      }
    }
  }

  public long request(Map paramMap1, Map paramMap2)
    throws IOException
  {
    DataPacket localDataPacket = requestDataPacket(paramMap1);
    long l = localDataPacket.getStatus();
    if (l != 0L)
      return l;
    NVRepository localNVRepository = localDataPacket.getNVRepository();
    int i = 0;
    if (i >= localNVRepository.size())
      return localDataPacket.getStatus();
    NameValuePair localNameValuePair = localNVRepository.elementAt(i);
    String str1 = localNameValuePair.getName();
    String str2;
    if (this.characterEncoding != null)
    {
      str2 = new String(localNameValuePair.getValue(), this.characterEncoding);
      log.debug("OUT(" + this.characterEncoding + "): [" + str1 + "]=[" + str2 + "]");
    }
    while (true)
    {
      paramMap2.put(str1, str2);
      i++;
      break;
      str2 = new String(localNameValuePair.getValue());
      log.debug("OUT: [" + str1 + "]=[" + str2 + "]");
    }
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.itrus.ica.IcaConnectionImpl
 * JD-Core Version:    0.6.2
 */